﻿using System;
using System.Collections.Generic;
using System.Data;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Data;
namespace UniMana.Win.Training
{
    public partial class FrmSchedule_SubChooseDay : DevExpress.XtraEditors.XtraForm
    {
        #region
        private DBMain m_DBMain = new DBMain();
        private int m_iDayOfWeek;
        private int m_iSession;
        private DataTable m_dtaScheduler;
        private List<Guid> m_lgListPersonel;
        private DataTable dt;
        private Guid m_gDefaultPersonelKey;
        private DataTable m_dteBackup;
        private int m_iTraining_SubjectOpenRequestKey = 0;
        private DateTime m_dteStar = new DateTime();
        private DateTime m_dteEnd = new DateTime();
        #endregion

        public FrmSchedule_SubChooseDay()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
        }

        public FrmSchedule_SubChooseDay(int _iDayOfWeek,
            int _iSession,
            DateTime _dteStar, 
            DateTime _dteEnd)
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
            this.m_iDayOfWeek = _iDayOfWeek;
            this.m_iSession = _iSession;

            this.m_dteStar = _dteStar;
            this.m_dteEnd = _dteEnd;

            LoadData();
        }

        public FrmSchedule_SubChooseDay(
            int _iDayOfWeek, 
            int _iSession, 
            List<Guid> _gListPersonel, 
            DateTime _dteStar, 
            DateTime _dteEnd, 
            int _iTraining_SubjectOpenRequestKey = 0)
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
            this.m_iDayOfWeek = _iDayOfWeek;
            this.m_iSession = _iSession;
            this.m_lgListPersonel = _gListPersonel;

            this.m_iTraining_SubjectOpenRequestKey = _iTraining_SubjectOpenRequestKey;

            this.m_dteStar = _dteStar;
            this.m_dteEnd = _dteEnd;
            dteEnd.DateTime = m_dteEnd;
            dteStart.DateTime = m_dteStar;

            LoadData();
        }

        private void LoadData()
        {
            dteEnd.DateTime = m_dteEnd;
            dteStart.DateTime = m_dteStar;

            this.m_dtaScheduler = new DataTable();
            this.m_dtaScheduler.Columns.Add("Date", typeof(DateTime));
            this.m_dtaScheduler.Columns.Add("WeekOfTerm", typeof(string));
            this.m_dtaScheduler.Columns.Add("Personnel_PersonnelKey", typeof(Guid));

            this.m_dteBackup = new DataTable();
            this.m_dteBackup.Columns.Add("Date", typeof(DateTime));
            this.m_dteBackup.Columns.Add("WeekOfTerm", typeof(string));
            this.m_dteBackup.Columns.Add("Personnel_PersonnelKey", typeof(Guid));

            dt = new DataTable();
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("WeekOfTerm", typeof(string));
            this.gctListWeek.DataSource = dt;
            if (this.m_iTraining_SubjectOpenRequestKey != 0)
            {
                DataTable listDay = 
                    this.m_DBMain.m_DCReadOnly.Personnel_Personnel_Sessions.GetSchedulerBySubjectRequestOpen(
                    this.m_iTraining_SubjectOpenRequestKey).ToDataTable();
                if (listDay.Rows.Count > 0)
                {
                    for (int i = 0; i < listDay.Rows.Count; i++)
                    {
                        Guid personnelKey = listDay.Rows[i]["Personnel_PersonnelKey"].ToGuid();
                        DateTime dteDate = listDay.Rows[i]["RegistTime"].ToDateTime();
                        this.m_dtaScheduler.Rows.Add(dteDate, "Tuần " + (i + 1),personnelKey);
                        this.m_dteBackup.Rows.Add(dteDate, "Tuần " + (i + 1), personnelKey);
                    }
                }
            }

            this.dteStart.EditValue = Cache.m_dteTeacherStart;
            this.dteEnd.EditValue = Cache.m_dteTeacherEnd;

            this.gctWeekChoose.DataSource = this.m_dtaScheduler;

            this.luePersonnel_Personnel.GetData(
                this.m_DBMain.m_DCReadOnly.Personnel_Personnels.GetListTeacher(
                this.m_lgListPersonel),
                displayMember:"FullName");
            if (this.m_lgListPersonel.Count == 1)
            {
                this.luePersonnel_Personnel.EditValue = this.m_lgListPersonel[0];
            }


        }

        public void OnResume(List<Guid> _lGPersonnel)
        {
            this.m_lgListPersonel = _lGPersonnel;
            this.luePersonnel_Personnel.GetData(
                this.m_DBMain.m_DCReadOnly.Personnel_Personnels.GetListTeacher(
                    this.m_lgListPersonel),
                "Personnel_PersonnelKey", "FullName");
            if (_lGPersonnel.Count == 1)
            {
                this.luePersonnel_Personnel.EditValue = _lGPersonnel[0];
            }
        }

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            DateTime dteStart = this.dteStart.DateTime;
            if ((int)dteStart.DayOfWeek != 1)
            {
                XtraMessageBox.Show("Ngày bắt đầu phải là thứ 2");
                dteStart = dteStart - TimeSpan.FromDays((int)dteStart.DayOfWeek - 1);
                this.dteStart.EditValue = dteStart;
                return;
            }
         //   this.dteEnd.DateTime = dteStart.AddDays(7 * 15);
            this.dteEnd.DateTime = dteStart;
        }

        private void btnView_Click(object sender, EventArgs e)
        {

            if (this.luePersonnel_Personnel.EditValue != null)
            {
                Cache.m_dteTeacherStart = this.dteStart.DateTime;
                Cache.m_dteTeacherEnd = this.dteEnd.DateTime;
                DateTime dteStart = this.dteStart.DateTime;
                DateTime dteEnd = this.dteEnd.DateTime;

                int iWeeks = (dteEnd - dteStart).Days / 7;

                while ((int)dteStart.DayOfWeek != this.m_iDayOfWeek)
                {
                    dteStart = dteStart.AddDays(1);
                }

                DataTable listDay =
                    this.m_DBMain.m_DCReadOnly.Personnel_Personnel_Sessions.GetScheduler(
                    this.m_iSession,
                    this.m_iDayOfWeek + 1,
                    this.luePersonnel_Personnel.EditValue.ToGuid(),
                    dteStart,
                    dteEnd,
                    this.ckeTimeBusy.Checked).
                    ToDataTable();

                if (listDay.Rows.Count > 0)
                {
                    dt = new DataTable();
                    dt.Columns.Add("Date", typeof(DateTime));
                    dt.Columns.Add("WeekOfTerm", typeof(string));
                    for (int i = 0; i < listDay.Rows.Count; i++)
                    {
                        dt.Rows.Add(listDay.Rows[i]["RegistTime"], "Tuần " + (i + 1));
                    }
                    this.gctListWeek.DataSource = dt;
                }
                else
                {
                    XtraMessageBox.Show("Không có ngày phù hợp cho giảng viên");
                }
            }
            //dt = new DataTable();
            //dt.Columns.Add("Date", typeof(DateTime));
            //dt.Columns.Add("WeekOfTerm", typeof(string));
            //for (int i = 0; i < iWeeks; i++)
            //{
            //    dt.Rows.Add(dteStart,"Tuần " + (i+1));
            //    dteStart = dteStart.AddDays(7);
            //}
            //this.gctListWeek.DataSource = dt;
            //int iDays = 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.grvListWeek.SelectedRowsCount > 0)
            {
                int[] row = this.grvListWeek.GetSelectedRows();
                for (int i = 0; i < row.Length; i++)
                {
                    DateTime date = 
                        (DateTime)this.grvListWeek.GetRowCellValue(
                            row[i], 
                            this.gclDate_Left);
                    string strWeek = 
                        this.grvListWeek.GetRowCellValue(
                            row[i], 
                            this.gclWeek_Left).ToString();
                    if (this.m_lgListPersonel.Count == 1)
                    {
                        this.m_dtaScheduler.Rows.Add(
                            date, 
                            strWeek,
                            this.m_lgListPersonel[0]);
                    }
                    else
                    {
                        this.m_dtaScheduler.Rows.Add(date, strWeek);
                    }
                }
                this.grvListWeek.DeleteSelectedRows();
                this.m_dtaScheduler = 
                    this.m_dtaScheduler.SortDataTable("Date");
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.grvWeekChoose.SelectedRowsCount > 0)
            {
                int[] row = this.grvWeekChoose.GetSelectedRows();
                for (int i = 0; i < row.Length; i++)
                {
                    DateTime dteDate = 
                        this.grvWeekChoose.GetRowCellValue(
                            row[i], 
                            this.gclDate_Right).ToDateTime();
                    string strWeek = 
                        this.grvWeekChoose.GetRowCellValue(
                            row[i], 
                            this.gclWeek_Right).ToString();
                    dt.Rows.Add(dteDate, strWeek);
                }
                this.grvWeekChoose.DeleteSelectedRows();
                this.dt = dt.SortDataTable("Date");
            }
        }

        public DataTable ListChooseDay
        {
            get { return this.m_dtaScheduler; }
        }

        public DataTable ListDayBackup
        {
            get { return this.m_dteBackup; }
        }

        private void FrmSubjectRequestOpen_ChooseDay_Load(object sender, EventArgs e)
        {

        }
    }
}